/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queues;

import com.sun.javafx.applet.Splash;
import javafx.application.Platform;
import javafx.application.Preloader;
import javafx.application.Preloader.ProgressNotification;
import javafx.application.Preloader.StateChangeNotification;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * Simple Preloader
 *
 * @author Renato Rosa <2101076@my.ipleiria.pt>
 */
public class QueuesPreloader extends Preloader {
    
    private ProgressBar bar;
    private Stage stage;
    private Label label;
    private Button button;
    
    private Scene createPreloaderScene() {
        bar = new ProgressBar();
        
        // a button to stop the loading at any time the user wants it
        button = new Button();
        button.setText("Sair");
        button.setAlignment(Pos.CENTER);
        button.setPrefWidth(50);
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                Platform.exit();
                System.exit(0);
            }
        });
        
        // the label to present information about the loading
        label = new Label();
        label.setAlignment(Pos.CENTER);
        label.setText("Inicializando...");
        BorderPane p = new BorderPane();
        p.setCenter(label);
        p.setBottom(button);
        BorderPane.setAlignment(button, Pos.BASELINE_CENTER);
        
        //p.setTop(label);
        return new Scene(p, 300, 150);        
    }
    
    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setScene(createPreloaderScene());        
        stage.show();
    }
    
    /*
    @Override
    public void handleStateChangeNotification(StateChangeNotification scn) {
        if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
            //stage.hide();
        }
    }
    */
    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        bar.setProgress(pn.getProgress());
    }

    @Override
    public void handleApplicationNotification(PreloaderNotification pn) {
        super.handleApplicationNotification(pn);
        // this is our notification
        if (pn instanceof RegistrationNotification) {
            RegistrationNotification rn = (RegistrationNotification) pn;
            if (rn.success) {
                //loaded! we can hide the preloader
                stage.hide();
            } else {
                //inform the user
                label.setText(rn.message);
            }  
        }
    }

    @Override
    public boolean handleErrorNotification(ErrorNotification en) {
        label.setText(en.getDetails());
        return true;
    }
    
    
    /**
     * Custom preloader notification
     */
    public static class RegistrationNotification implements Preloader.PreloaderNotification {
        public boolean success;
        public String message;

        /**
         * 
         * @param success whether the notification means success or error
         * @param message the message to show
         */
        public RegistrationNotification(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
    }
}
